package org.feng.demofx.util;

import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.Callback;
import org.feng.demofx.sys.TableColumnDefine;
import org.feng.demofx.sys.TableColumnDesc;

import java.lang.reflect.Field;
import java.util.*;

/**
 * 表字段工具
 *
 * @author feng
 */
public class TableColumnUtil {

    private static final Map<Class<?>, Set<TableColumnDefine>> TABLE_COLUMN_DEFINES_MAP = new HashMap<>();

    @Deprecated
    public static <T> TableColumn<T, Boolean> genSelectedColumn(String title, String selectedFieldName) {
        TableColumn<T, Boolean> selectColumn = new TableColumn<>(title);
        selectColumn.setCellValueFactory(new PropertyValueFactory<>(selectedFieldName));
        selectColumn.setCellFactory(param -> {
            CheckBoxTableCell<T, Boolean> cell = new CheckBoxTableCell<>();
            cell.setAlignment(Pos.CENTER);
            return cell;
        });

        return selectColumn;
    }

    public static <T> void parseColumnsByClass(ObservableList<TableColumn<T, ?>> columns, Class<T> clazz) {

        // 解析注解TableColumnDesc
        if (!TABLE_COLUMN_DEFINES_MAP.containsKey(clazz)) {
            Field[] declaredFields = clazz.getDeclaredFields();
            Set<TableColumnDefine> tableColumnDefineSet = new TreeSet<>(Comparator.comparingInt(TableColumnDefine::getOrder)
                    .thenComparing(TableColumnDefine::getFieldName));

            for (Field declaredField : declaredFields) {
                TableColumnDesc tableColumnDesc = declaredField.getAnnotation(TableColumnDesc.class);
                if (Objects.isNull(tableColumnDesc)) {
                    continue;
                }

                tableColumnDefineSet.add(new TableColumnDefine(tableColumnDesc, declaredField.getName()));
                TABLE_COLUMN_DEFINES_MAP.put(clazz, tableColumnDefineSet);
            }
        }

        // 创建表列
        Set<TableColumnDefine> tableColumnDefines = TABLE_COLUMN_DEFINES_MAP.get(clazz);
        for (TableColumnDefine tableColumnDefine : tableColumnDefines) {
            // 设置表头
            TableColumn<T, String> colum = new TableColumn<>(tableColumnDefine.getColumnDesc());
            // 设置具体的列进行单元格编辑
            // colum.setCellFactory(TextFieldTableCell.forTableColumn());
            // 设置表格列的具体属性
            colum.setCellValueFactory(new PropertyValueFactory<>(tableColumnDefine.getFieldName()));
//            colum.setOnEditCommit(event -> {
//                TableView tempTable = event.getTableView();
//                T object = (T)tempTable.getItems().get(event.getTablePosition().getRow());
//                try {
//                    Field declaredField = clazz.getDeclaredField(tableColumnDefine.getFieldName());
//                    declaredField.setAccessible(true);
//                    declaredField.set(object, event.getNewValue());
//                } catch (NoSuchFieldException | IllegalAccessException e) {
//                    throw new RuntimeException(e);
//                }
//            });
            columns.add(colum);
        }
    }
}
